<template>
  <div class="detail">
    <header>
      <div class="header-return" v-show="isShow">
        <i class="iconfont icon-return" @click="goBack"></i>
        <i class="iconfont icon-home" @click="goHome"></i>
      </div>
      <div class="header-bar" v-show="!isShow" :style="styleOption">
        <i class="iconfont icon-return" @click="goBack"></i>
        <span>商品详情</span>
        <span>商品评价</span>
        <i class="iconfont icon-home" @click="goHome"></i>
      </div>
    </header>
    <section ref="wrapper">
      <div>
        <!-- swiper -->
        <div class="swiper-main">
          <swiper :options="swiperOption">
            <swiper-slide v-for="(item, index) in swiperLists" :key="index">
              <img :src="item.imgUrl" atl="" />
            </swiper-slide>
          </swiper>

          <!-- 以下看需要添加 分页器 -->
          <div class="swiper-pagination"></div>
        </div>
        <div class="goods-name">
          <h1>{{ goods.name }}</h1>
          <div>口感清淡无负担的日常品饮。</div>
        </div>
        <div class="goods-price">
          <div class="oprice">
            <span>￥</span>
            <p>{{ goods.price }}</p>
            <div class="weight">
              <span>/</span>
              <span>150g</span>
            </div>
          </div>
          <div class="pprice">
            <span>价格:</span>
            <del>￥599</del>
          </div>
        </div>
        <div>
          <img :src="goods.imgUrl" style="width: 100%" />
        </div>
      </div>
    </section>
    <footer>
      <div class="follow">
        <ul>
          <li>
            <i class="iconfont icon-kefu"></i>
            <span>客服</span>
          </li>
          <li>
            <i class="iconfont icon-gouwuche" @click="goCart"></i>
            <span>购物车</span>
          </li>
          <li>
            <i class="iconfont icon-shoucang"></i>
            <span>收藏</span>
          </li>
        </ul>
      </div>
      <div class="plan">
        <ul>
          <li class="sho-car" @click="addCart">加入购物车</li>
          <li>立即购买</li>
        </ul>
      </div>
    </footer>
  </div>
</template>

<script>
import "swiper/dist/css/swiper.css";
import { swiper, swiperSlide } from "vue-awesome-swiper";
import http from "@/common/api/request.js";
import { Toast } from "mint-ui";
//引入插件
import BetterScroll from "better-scroll";

export default {
  name: "Detail",
  components: {
    swiper,
    swiperSlide,
  },
  data() {
    return {
      watchId: null,
      goods: {},
      id: null,
      isShow: true,
      BetterScroll: [],
      styleOption: {},
      swiperLists: [
        {
          imgUrl:
            "https://img.tea7.com/0156515_0.jpeg?x-oss-process=image/resize,w_500",
        },
        {
          imgUrl:
            "https://img.tea7.com/0012978_0.jpeg?x-oss-process=image/resize,w_500",
        },
        {
          imgUrl:
            "https://img.tea7.com/0012979_0.jpeg?x-oss-process=image/resize,w_500",
        },
        {
          imgUrl:
            "https://img.tea7.com/0012992_0.jpeg?x-oss-process=image/resize,w_500",
        },
        {
          imgUrl:
            "https://img.tea7.com/0012980_0.jpeg?x-oss-process=image/resize,w_500",
        },
      ],

      swiperOption: {
        //swiper3
        autoplay: 3000,
        speed: 1000,
        pagination: {
          el: ".swiper-pagination",
          //修改显示样式
          type: "fraction",
        },
      },
    };
  },
  // watch:{
  //  watchId(){

  //    if(this.$route.query.id != this.watchId){
  // 	   console.log(333)
  // 	   this.getData()
  // 	    this.watchId = this.$route.query.id
  //    }
  //  }
  // },
  created() {
    this.watchId = this.$route.query.id;
    this.getData();
  },
  mounted() {
    this.initBs();
  },
  activated() {
    if (this.$route.query.id != this.watchId) {
      console.log("id 不同了");
      this.getData();
      this.watchId = this.$route.query.id;
    }
  },
  methods: {
    //去购物车
    goCart() {
      this.$router.push("/cart");
    },
    //返回首页
    goHome() {
      this.$router.push({
        name: "Home",
      });
    },
    //返回
    goBack() {
      this.$router.back();
    },
    //请求数据
    async getData() {
      let id = this.$route.query.id;
      let res = await http.$axios({
        url: "/api/goods/id",
        params: {
          id,
        },
      });
      this.goods = res[0];
    },
    //滚动
    initBs() {
      this.BetterScroll = new BetterScroll(this.$refs.wrapper, {
        probeType: 3,
        bounce: false, //阻止scroll回弹
        click: true,
        movale: true,
        zoom: true,
        observeDOM: true, //解决刷新后依然有滚动效果
      });
      //
      this.BetterScroll.on("scroll", (pos) => {
        //在scroll大于200 时切换
        let posY = Math.abs(pos.y);
        this.isShow = posY >= 200 ? false : true;

        //header透明度样式切换过度效果
        let opacity = posY / 500;
        opacity = opacity > 1 ? 1 : opacity;

        this.styleOption = {
          opacity: opacity,
        };
      });
    },

    //添加购物车
    addCart() {
      let userInfo = JSON.parse(localStorage.getItem("teaUseraInfor"));
      console.log(userInfo);

      let id = this.$route.query.id;
      console.log(id);
      http
        .$axios({
          url: "/api/addCart",
          method: "post",
          data: {
            goodsId: id,
          },
          headers: {
            token: true,
          },
        })
        .then((res) => {
          console.log("res:", res);
          if (res.success) {
            Toast(res.msg);
          }
        });
    },
  },
};
</script>

<style scoped lang="less">
.detail {
  display: flex;
  flex-direction: column;
  width: 100vw;
  height: 100vh;
  overflow: hidden;

  header {
    position: fixed;
    left: 0;
    top: 0;
    z-index: 9999;
    width: 100%;
    height: 1rem;

    .header-return {
      align-items: center;
      display: flex;
      justify-content: space-between;
      align-items: center;
      width: 100%;
      height: 1rem;

      i {
        font-size: 0.5rem;
        // padding:0.15rem;
        margin: 0 0.1rem;
        width: 0.7rem;
        height: 0.7rem;
        line-height: 0.7rem;
        background-color: rgba(0, 0, 0, 0.3);
        border-radius: 50%;
        text-align: center;
        color: #fff;
      }
    }

    .header-bar {
      align-items: center;
      display: flex;
      justify-content: space-around;
      align-items: center;
      width: 100%;
      height: 1rem;
      background-color: #fff;

      i {
        font-size: 0.5rem;
      }
      span {
        font-size: 0.45rem;
      }
    }
  }

  section {
    flex: 1;
    overflow: hidden;
  }

  .swiper-main {
    position: relative;
    width: 100%;
    height: 10rem;
    // margin-top: 2.46rem;
    /* background-color: blue; */

    .swiper-container {
      width: 100%;
      height: 10rem;
    }
    .swiper-pagination {
      width: 10%;
      bottom: 0.1875rem;
      text-align: right;
      right: 0.25rem;
      color: #fff;
      font-size: 0.3rem;
      background-color: #999;
      left: 8.5rem;
      text-align: center;
      padding: 0.02rem 0;
      border-radius: 5px;
    }
    /deep/ .swiper-pagination-bullet-active {
      background-color: #b0352f;
    }
    /deep/ .swiper-pagination-bullet {
      margin: 0 0.08rem;
    }

    img {
      width: 100%;
      height: 100%;
    }
  }

  .goods-name {
    margin: 0.4rem 0.3125rem;
    padding-bottom: 0.4rem;
    border-bottom: 1px solid #ccc;

    h1 {
      font-size: 0.45rem;
      font-weight: 500;
    }
    div {
      padding: 0.1rem 0;
      font-size: 0.3rem;
      color: #999;
    }
  }

  .goods-price {
    padding: 0.026rem 0.3125rem;

    .oprice {
      color: #e11;
      display: flex;
      align-items: flex-end;
      font-size: 0.5rem;
      span {
        font-size: 0.32rem;
      }
      .weight {
        color: #999;
        font-size: 0.2rem;
        margin-left: 0.125rem;
      }
    }
    .pprice {
      color: #999;
      font-size: 0.37rem;
    }
  }

  footer {
    border-top: 1px solid #f2f2f2;
    display: flex;
    position: fixed;
    bottom: 0;
    left: 0;
    width: 100%;
    height: 1.5rem;
    background-color: #fff;

    .follow {
      width: 40%;
      ul {
        display: flex;
        width: 100%;
        height: 100%;
        // border:1px solid #e22;
        font-size: 0.25rem;
        display: flex;
        // justify-content: center;
        // align-items: center;
        text-align: center;

        li {
          display: flex;
          flex-direction: column;
          justify-content: center;
          align-items: center;
          color: #000;
          width: 50%;
          border-right: 1px solid #f2f2f2;
          img {
            width: 50px;
          }
        }
      }
    }
    .plan {
      width: 60%;
      ul {
        display: flex;
        justify-content: center;
        text-align: center;
        width: 100%;
        height: 100%;
        background-color: #dd2727;
        font-size: 0.4rem;
        color: #fff;
        li {
          width: 50%;
          line-height: 1.5rem;

          &.sho-car {
            background-color: #ff9500;
          }
        }
      }
    }
  }
}
</style>
